package edu.nf.api.controller.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import edu.nf.api.controller.BaseController;
import edu.nf.api.controller.alipay.config.AlipayConfig;
import edu.nf.util.alipay.DateUtile;
import edu.nf.waimai.dao.order.OrderDAO;
import edu.nf.waimai.dao.order.impl.OrderDAOImpl;
import edu.nf.waimai.model.order.Order;
import edu.nf.waimai.service.user.ShoppingService;
import edu.nf.waimai.service.user.impl.ShoppingServiceImpl;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.ForwardView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Date 2023-06-26
 * @Author qiu
 * <p>
 * 判断是否支付成功，成功返回成功的页面
 */
public class ReturnUrlController extends BaseController {

    @RequestMapping("/alipay/returnUrl")
    public View returnUrlController(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws UnsupportedEncodingException {
        // 获取支付宝GET过来反馈信息
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // 乱码解决，这段代码在出现乱码时使用
            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        boolean signVerified = false;
        try {
            signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset,
                    AlipayConfig.sign_type);
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 调用SDK验证签名
        // 是否支付成功！
        // 转发视图
        ForwardView forwardView = null;
        if (signVerified) {

            // 相应的业务功能
            // 添加订单信息......
//            Order order = (Order)session.getAttribute("order");
//            OrderDAO dao = new OrderDAOImpl();
//            dao.updateOrder(order.orderId,1);
//
//            //把购物车的状态修改
//            ShoppingService service = new ShoppingServiceImpl();
//            service.updateShopPing(order.getUid(), order.getShopId());

            // 转发视图
            forwardView = new ForwardView("http://localhost:9090/orderDetails");
            // 将数据放到请求作用域
            forwardView.addObject("xxxx", "xxxx");


        }
        return forwardView;
    }

}
